Ext.define('Zm.controller.User', {
    extend: 'Ext.app.Controller',
    stores: ['Users'],
    views: ['user.Edit'],
    refs:[
        {ref:'UserList',selector:'userlist'}
    ],
    
    init: function(){
        var me = this;
        me.control({
            'userlist button[action=useredit]':{
                click: me.editUser
            },
            'userlist button[action=useradd]':{
                click: me.addUser
            },
            'userlist button[action=userdel]':{
                click: me.delUser
            },
            'userlist':{
                itemdblclick: me.editUser
            },
            'useredit button[action=save]':{
                click: me.saveUser
            }
        });
    },
    onLaunch: function(){
    },
    editUser:function(){
        var me = this,view,
            sm = me.getUserList().getSelectionModel(),
            records = sm.getSelection();
        if(records.length>0){
            view = Ext.widget('useredit');
            view.down('form').loadRecord(records[0]);
        }
    },
    addUser:function(){
        var view = Ext.widget('useredit');
    },
    delUser:function(){
        var me = this,
            lang = Zm.i18n.Lang,
            sm = me.getUserList().getSelectionModel(),
            records = sm.getSelection();
        if(records.length>0){
            Ext.MessageBox.show({
                   title:lang.tDel,
                   msg:lang.tDelUserMsg,
                   buttons: Ext.Msg.YESNO,
                   fn: me.onDelCallback,
                   scope: me,
                   icon: Ext.Msg.QUESTION
               });                        
        }
    },
    saveUser:function(button){
        var me = this,
            cm = Zm.Common,
            lang = Zm.i18n.Lang,
            win = button.up('window'),
            form = win.down('form').getForm(),
            newpass = form.findField('passwd').getValue(),
            renewpass = form.findField('repasswd').getValue();
            
        if(newpass != renewpass){
            cm.errMsg(lang.tError,lang.tReErr);
            return;
        }
        form.submit({
            url: g_app + '/Users/save',
            scope: this,
            success: function(form,action){
                me.getUsersStore().load();
                win.close();
            },
            failure: cm.onFail
        });
    },
    onDelCallback: function(btn){
        var me = this,ids='-1',i,
            lang = Zm.i18n.Lang,
            cm = Zm.Common,
            sm = me.getUserList().getSelectionModel(),
            records = sm.getSelection();
        if(btn == 'yes'){
            for(i=0;i<records.length;i++){
                ids += ","+ records[i].get('userid');
            }
            Ext.Ajax.request({
                url: g_app + '/Users/del',
                params: {ids: ids},
                scope: me,
                success:function(response,options){ 
                    me.getUsersStore().load();                            
                },
                failure:cm.onAFail
             });
            
        }
    }
});